using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Macaw.Wss3.DependencySpiderSolution
{
    class DependencySpiderJobFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureActivated start", this.GetType().FullName));
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;

            // Make sure the job isn't already registered.
            foreach (SPJobDefinition oldjob in webapp.JobDefinitions)
            {
                if (oldjob.Name == DependencySpiderJobDefinition.JobName)
                    oldjob.Delete();
            }


            #region Daily Schedule
            DependencySpiderJobDefinition job = new DependencySpiderJobDefinition(webapp);
            SPDailySchedule dailyschedule = new SPDailySchedule();
            dailyschedule.BeginHour = 0;
            dailyschedule.BeginMinute = 0;
            dailyschedule.BeginSecond = 0;

            dailyschedule.EndHour = 23;
            dailyschedule.EndMinute = 59;
            
            job.Schedule = dailyschedule;
            job.Update();
            #endregion

            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureActivated end", this.GetType().FullName));
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureDeactivating start", this.GetType().FullName));
            SPWebApplication webapp = properties.Feature.Parent as SPWebApplication;

            // Delete the job.
            foreach (SPJobDefinition oldjob in webapp.JobDefinitions)
            {
                if (oldjob.Name == DependencySpiderJobDefinition.JobName)
                    oldjob.Delete();

                if (oldjob.Name == DependencySpiderJobDefinition.JobName+"_now")
                    oldjob.Delete();
            }

            System.Diagnostics.Trace.WriteLine(string.Format("{0}: FeatureDeactivating end", this.GetType().FullName));
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
